<?php 
class exportCurrentAction extends AdminMaster{	
	public function execute($request){
		$id = $request->getParameter('id', '-1');
		$action = $request->getParameter('action_name', '');
		
		$job = JobsTable::getDataById($id);
		if($action == ADMIN_DEFINED::EXPORT && count($job)){
			$company = CompaniesTable::getDataById($job['company_id']);
			//echo sfConfig::get('app_excel_candidate-title')
			$ex = new PHPExcel();
			$ex->getProperties()->setCreator(sfConfig::get('app_excel_creator'))
				->setLastModifiedBy(sfConfig::get('app_excel_creator'))
				->setTitle(sfConfig::get('app_excel_candidate-title'))
				->setSubject(sfConfig::get('app_excel_candidate-title'))
				->setDescription(sfConfig::get('app_excel_candidate-title'));
			$ex->setActiveSheetIndex(0)->setTitle(sfConfig::get('app_excel_candidate-title'));
			$ex->getActiveSheet()
				->mergeCells('B2:G2')
				->setCellValue('B2',$job['title'].' ('.$company['company_name'].')')
				
				->setCellValue('B3',__('date_apply', null, 'excel_list'))
				->setCellValue('C3',__('date_interview', null, 'excel_list'))
				->setCellValue('D3',__('full_name', null, 'excel_list'))
				->setCellValue('E3',__('address', null, 'excel_list'))
				->setCellValue('F3',__('phone_number', null, 'excel_list'))
				->setCellValue('G3',__('status', null, 'excel_list'));
				
			
			
			$interview = CandidateToJobsTable::getListByJobId($id);
			$count_i = 3;
			foreach($interview as $m){ 
				$count_i++;
				$ex->getActiveSheet()
					->setCellValue('B'.$count_i, DateHelper::dateFormatFree($m['date_apply'], 'Y-m-d'))
					->setCellValue('C'.$count_i, DateHelper::dateFormatFree($m['date_interview'], 'Y-m-d'))
					->setCellValue('D'.$count_i, $m['Candidates']['full_name'])
					->setCellValue('E'.$count_i, $m['Candidates']['address'])
					->setCellValue('F'.$count_i, $m['Candidates']['phone_number'])
					->setCellValue('G'.$count_i, __('result_status_'.str_replace('-', '_', $m['result_status']), null, 'array_data'));
					
			}
			//APPLY STYLE
			$style = array(
					'borders' => array(
							'allborders' => array(
									'style' => PHPExcel_Style_Border::BORDER_THIN
							)
					)
			);
			$ex->getActiveSheet()->getStyle('B3:G'.$count_i)
			->applyFromArray($style);
			
			$style = array(
					'font' => array(
							'bold' =>true,
							'size' => '13'
					),
					'alignment' => array(
							'horizontal'	=> PHPExcel_Style_Alignment::HORIZONTAL_CENTER
					)
			);
			$ex->getActiveSheet()->getStyle('B2')
			->applyFromArray($style);
			
			$style = array(
					'fill' => array(
							'type' => PHPExcel_Style_Fill::FILL_SOLID,
							'color'=>  array(
									'rgb'	=> 'dedede'
							)
					),
					'font' => array(
							'color'=>  array(
									'rgb'	=> '0000ff'
							)
					),
			);
			$ex->getActiveSheet()->getStyle('B3:G3')
			->applyFromArray($style);
			//->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
			//END
			
			$exIO = PHPExcel_IOFactory::createWriter($ex, 'Excel2007');
			ob_end_clean();
			
			header('Content-Type: application/vnd.ms-excel');
			header('Content-Disposition: attachment;filename="'.sfConfig::get('app_excel_file-name').'.'.date('Y-m-d',time()).'.xlsx"');
			header('Cache-Control: max-age=0');
			
			$exIO->save('php://output');die;
		}
		$this->redirect(url_for2('preview_index'));
	}
}